using System;
using System.Globalization;
using System.Web.UI.WebControls;
using mojoPortal.Web;
using mojoPortal.Web.Framework;
using mojoPortal.Web.UI;
using mojoPortal.Business;
using mojoPortal.Business.WebHelpers;
using eStore.Business;
using eStore.Helpers;
using Resources;
using System.Collections;

namespace eStore.UI
{
    public partial class AdminDashboardPage : mojoBasePage
    {
        private int pageId = -1;
        private int moduleId = -1;
        private Store store;
        private eStoreConfiguration config = new eStoreConfiguration();

        protected void Page_Load(object sender, EventArgs e)
        {
            LoadSettings();
            if (!UserCanEditModule(moduleId))
            {
                SiteUtils.RedirectToAccessDeniedPage();
                return;
            }

            SetupCss();
            PopulateLabels();
            EnsureReportData();
        }

        private void EnsureReportData()
        {
            if (store == null) { return; }
            if (pageId == -1) { return; }
            if (moduleId == -1) { return; }
            if (Page.IsPostBack) { return; }

            decimal storeRevenue = Order.GetAllTimeRevenueTotal(store.Guid);
            decimal reportRevenue = CommerceReport.GetAllTimeRevenueByModule(store.ModuleGuid);

            if ((storeRevenue > 0) && (reportRevenue == 0))
            {
                CommerceReport.DeleteByModule(store.ModuleGuid);
                Order.EnsureSalesReportData(store.ModuleGuid, pageId, moduleId);
                SiteUser.UpdateTotalRevenue();
            }


        }


        private void PopulateLabels()
        {
            Title = SiteUtils.FormatPageTitle(siteSettings, eStoreResources.StoreManagerHeading);

            ltrTitle.Text = eStoreResources.StoreManagerHeading;
            lnkStoreSettings.Text = eStoreResources.StoreSettingsLink;
            lnkProductAdmin.Text = eStoreResources.ProductAdministrationLink;
            lnkCategoryAdmin.Text = eStoreResources.CategoryAdministrationLink;
            lnkDiscountAdmin.Text = eStoreResources.DiscountAdministration;
            lnkOrderHistory.Text = eStoreResources.OrderHistoryAdminLink;
            lnkBrowseCarts.Text = eStoreResources.BrowseCartsLink;
            btnRebuildReports.Text = eStoreResources.RebuildReportsButton;
            btnRebuildReports.Visible = WebConfigSettings.ShowRebuildReportsButton && WebUser.IsAdmin;

            if ((store == null) || (!WebUser.IsInRoles(siteSettings.CommerceReportViewRoles))) return;
            liReports.Visible = true;
            lnkReports.Text = eStoreResources.ReportsLink;
            lnkReports.NavigateUrl = SiteRoot + "/eStore/AdminSalesByModule.aspx?m=" + store.ModuleGuid;
        }

        void btnRebuildReports_Click(object sender, EventArgs e)
        {
            //Module m = new Module(moduleId);
            //if (m.SiteGuid != siteSettings.SiteGuid) { return; }
            //CommerceReport.DeleteByModule(m.ModuleGuid);
            //Order.EnsureSalesReportData(m.ModuleGuid, pageId, m.ModuleId);
            //SiteUser.UpdateTotalRevenue();
            if (store != null)
            {
                CommerceReport.DeleteByModule(store.ModuleGuid);
                Order.EnsureSalesReportData(store.ModuleGuid, pageId, moduleId);
                SiteUser.UpdateTotalRevenue();
            }

            WebUtils.SetupRedirect(this, Request.RawUrl);

        }

        private void LoadSettings()
        {
            if (CurrentPage.ContainsModule(moduleId))
            {
                //store = StoreHelper.GetStore(siteSettings.SiteGuid, moduleId);
                store = StoreHelper.GetStore();
            }
            lnkStoreSettings.Visible = config.ShowStoreSetting;
            lnkStoreSettings.NavigateUrl = SiteRoot + "/eStore/AdminStoreSettings.aspx?pageid="
                + pageId.ToString(CultureInfo.InvariantCulture) + "&mid=" + moduleId.ToInvariantString();

            lnkProductAdmin.Visible = config.ShowProductAdminSetting;
            lnkProductAdmin.NavigateUrl = SiteRoot + "/eStore/AdminProduct.aspx?pageid="
                + pageId.ToString(CultureInfo.InvariantCulture) + "&mid=" + moduleId.ToInvariantString();

            //lnkCategoryAdmin.Visible = config.ShowCategorySetting;
            lnkCategoryAdmin.NavigateUrl = SiteRoot + "/eStore/AdminCategory.aspx?pageid="
                + pageId.ToString(CultureInfo.InvariantCulture) + "&mid=" + moduleId.ToInvariantString();

            lnkDiscountAdmin.Visible = config.ShowDiscountAdminSetting;
            lnkDiscountAdmin.NavigateUrl = SiteRoot + "/eStore/AdminDiscounts.aspx?pageid="
                + pageId.ToString(CultureInfo.InvariantCulture) + "&mid=" + moduleId.ToInvariantString();

            lnkOrderHistory.Visible = config.ShowOrderHistorySetting;
            lnkOrderHistory.NavigateUrl = SiteRoot + "/eStore/AdminOrderHistory.aspx?pageid="
                + pageId + "&mid=" + moduleId.ToInvariantString();

            lnkBrowseCarts.Visible = config.ShowBrowseCartSetting;
            lnkBrowseCarts.NavigateUrl = SiteRoot + "/eStore/AdminCartBrowser.aspx?pageid="
                + pageId + "&mid=" + moduleId.ToInvariantString();



        }

        protected virtual void SetupCss()
        {
            // older skins have this
            if (Page.Master == null) return;
            StyleSheet stylesheet = (StyleSheet)Page.Master.FindControl("StyleSheet");
            if (stylesheet == null) return;
            if (stylesheet.FindControl("styleestore") != null) return;
            Literal cssLink = new Literal
                                  {
                                      ID = "styleestore",
                                      Text = @"<link href='"
                                             + SiteUtils.GetSkinBaseUrl()
                                             + @"styleestore.css' type='text/css' rel='stylesheet' media='screen' />"
                                  };

            stylesheet.Controls.Add(cssLink);
        }


        #region OnInit

        protected override void OnPreInit(EventArgs e)
        {
            AllowSkinOverride = true;
            base.OnPreInit(e);
        }

        protected override void OnInit(EventArgs e)
        {
            LoadParams();
            LoadPanels();
            base.OnInit(e);

            Load += Page_Load;
            btnRebuildReports.Click += btnRebuildReports_Click;

            SuppressPageMenu();
        }

        private void LoadParams()
        {

            pageId = WebUtils.ParseInt32FromQueryString("pageid", pageId);
            moduleId = WebUtils.ParseInt32FromQueryString("mid", true, moduleId);

        }

        private void LoadPanels()
        {
            Hashtable settings = ModuleSettings.GetModuleSettings(moduleId);
            config = new eStoreConfiguration(settings);
            LoadSideContent(false, false, true, true, 0);
        }


        #endregion

    }
}
